<template>
  <el-dialog
    :before-close="close"
    :width="dialogWidth"
    :title="title"
    :visible.sync="isShow"
    v-if="isShow"
    v-dialogDrag
  >
    <el-form :model="form" ref="form" :rules="rules">
      <el-form-item label="中心名：" prop="name" :label-width="formLabelWidth">
        <el-input
          v-model="form.name"
          autocomplete="off"
          placeholder="请输入中心名"
          @keyup.enter.native="handleConfirm('form')"
        ></el-input>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer align-center">
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" @click="handleConfirm('form')">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import "../utils/drag.js";

export default {
  name: "center_dialog",
  data() {
    var validateName = (rule, value, callback) => {
      if (!value) {
        return callback(new Error("请输入中心名！"));
      } else if (value.search(/^[\u0391-\uFFE5]+$/) === -1) {
        return callback(new Error("请输入中文！"));
      } else if (value.length < 2) {
        return callback(new Error("长度在 2-10 个汉字！"));
      } else if (value.length > 10) {
        return callback(new Error("长度在 2-10 个汉字！"));
      } else {
        callback();
      }
    };
    return {
      form: {
        name: ""
      },
      rules: {
        name: { validator: validateName, trigger: "blur" }
      },
      formLabelWidth: "100px",
      dialogWidth: "400px"
    };
  },
  computed: {
    isShow() {
      return this.addDialog || this.editDialog;
    }
  },
  watch: {
    editDialog() {
      this.form = {
        name: this.centerForm.name
      };
    }
  },
  methods: {
    handleConfirm(formName) {
      // 先进行表单验证
      this.$refs[formName].validate(valid => {
        if (valid) {
          this.editDialog ? this.editCenter() : this.addCenter();
        }
      });
    },
    addCenter() {
      this.$axios.post("/api/system/addCenter", this.form).then(data => {
        if (data.status) {
          this.$message({
            type: "success",
            message: "添加成功！"
          });
          this.$emit("update");
          this.close();
        } else {
          this.$message({
            type: "error",
            message: data.msg
          });
        }
      });
    },
    editCenter() {
      this.$axios
        .put("/api/system/editCenter/" + this.centerForm.id, this.form)
        .then(data => {
          if (data.status) {
            this.$message({
              type: "success",
              message: "编辑成功！"
            });
            this.$emit("update");
            this.close();
          } else {
            this.$message({
              type: "error",
              message: data.msg
            });
          }
        });
    },
    close() {
      this.$emit("close");
      this.resetForm();
    },
    resetForm() {
      this.form = {};
    }
  },
  props: ["addDialog", "editDialog", "title", "centerForm"]
};
</script>
<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
</style>
